//
//  LGFollowPressureView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/26.
//

#import "LGFollowPressureView.h"
#import "DatePickerTwoView.h"
#define baseWidth     (Screen_W-viewPix(56))
#define dateWidth     (114/320.0)*baseWidth
#define itemWidth     (76/320.0)*baseWidth
#define heartWidth    (53/320.0)*baseWidth

@interface LGFollowPressureView()<DatePickerViewDelegate>
/**   */
@property (nonatomic , strong)UILabel *dateTitle;
/**   */
@property (nonatomic , strong)UIButton *dateButton;
/**   */
@property (nonatomic , strong)UIImageView *tipView;
/**   */
@property (nonatomic , strong)UILabel *highTitle;
/**   */
@property (nonatomic , strong)UITextField *highTF;
/**   */
@property (nonatomic , strong)UILabel *lowTitle;
/**   */
@property (nonatomic , strong)UITextField *lowTF;
/**   */
@property (nonatomic , strong)UILabel *heartTitle;
/**   */
@property (nonatomic , strong)UITextField *heartTF;
/**   */
@property (nonatomic , strong)UIView *segmentView;
/**   */
@property (nonatomic , strong)UIView *lineView1;
/**   */
@property (nonatomic , strong)UIView *lineView2;
/**   */
@property (nonatomic , strong)UIView *lineView3;

@end

@implementation LGFollowPressureView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setCanEdit:(BOOL)canEdit{
    _canEdit = canEdit;
    self.highTF.placeholder = (canEdit == YES)?@"请输入":@"";
    self.lowTF.placeholder = (canEdit == YES)?@"请输入":@"";
    self.heartTF.placeholder = (canEdit == YES)?@"请输入":@"";
    self.dateButton.userInteractionEnabled = canEdit;
    self.highTF.userInteractionEnabled = canEdit;
    self.lowTF.userInteractionEnabled = canEdit;
    self.heartTF.userInteractionEnabled = canEdit;
    self.tipView.hidden = !canEdit;
    if(canEdit == YES){
        self.dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }else{
        self.dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    }
}

-(void)setDataDic:(NSMutableDictionary *)dataDic{
    _dataDic = dataDic;
    self.highTF.text = LGNSString(dataDic[@"sbp"]);
    self.lowTF.text = LGNSString(dataDic[@"dbp"]);
    self.heartTF.text = LGNSString(dataDic[@"pulse"]);
    NSString *dateString = LGNSString(dataDic[@"bpdate"]);
    NSString *colorString = (dateString.length>0)?@"#666666":@"#BBBBBB";
    NSString *title = (dateString.length>0)?dateString:@"请选择...";
    [self.dateButton setTitleColor:[UIColor colorWithString:colorString] forState:UIControlStateNormal];
    [self.dateButton  setTitle:title forState:UIControlStateNormal];
}

//选择日期
-(void)selectDateAction{
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = self.dateButton.titleLabel.text;
    pickerView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    [self.dataDic setObject:date forKey:@"bpdate"];
    [self.dateButton  setTitle:date forState:UIControlStateNormal];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    if(textField == self.highTF){
        [self.dataDic setObject:textField.text forKey:@"sbp"];
    }else if (textField == self.lowTF){
        [self.dataDic setObject:textField.text forKey:@"dbp"];
    }else{
        [self.dataDic setObject:textField.text forKey:@"pulse"];
    }
}

-(void)createSubView{
    [self addSubview:self.dateTitle];
    [self addSubview:self.dateButton];
    [self.dateButton addSubview:self.tipView];
    [self addSubview:self.highTitle];
    [self addSubview:self.highTF];
    [self addSubview:self.lowTitle];
    [self addSubview:self.lowTF];
    [self addSubview:self.heartTitle];
    [self addSubview:self.heartTF];
    [self addSubview:self.segmentView];
    [self addSubview:self.lineView1];
    [self addSubview:self.lineView2];
    [self addSubview:self.lineView3];
}

-(UILabel *)dateTitle{
    if(!_dateTitle){
        _dateTitle = [UILabel lableWithText:@"日期" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        _dateTitle.frame = CGRectMake(0, 0, dateWidth, viewPix(36));
    }
    return _dateTitle;
}

-(UILabel *)highTitle{
    if(!_highTitle){
        _highTitle = [UILabel lableWithText:@"收缩压" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        _highTitle.frame = CGRectMake(kMaxX(self.dateTitle.frame), 0, itemWidth, viewPix(36));
    }
    return _highTitle;
}

-(UILabel *)lowTitle{
    if(!_lowTitle){
        _lowTitle = [UILabel lableWithText:@"舒张压" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        _lowTitle.frame = CGRectMake(kMaxX(self.highTitle.frame), 0, itemWidth, viewPix(36));
    }
    return _lowTitle;
}

-(UILabel *)heartTitle{
    if(!_heartTitle){
        _heartTitle = [UILabel lableWithText:@"心率" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        _heartTitle.frame = CGRectMake(kMaxX(self.lowTitle.frame), 0, viewPix(53), viewPix(36));
    }
    return _heartTitle;
}

-(UIButton *)dateButton{
    if(!_dateButton){
        _dateButton = [UIButton buttonWithTitle:@"请选择..." titleFont:LGFont(14) normalColor:@"#BBBBBB" selectColor:@"#333333"];
        _dateButton.frame = CGRectMake(0, viewPix(36), dateWidth, viewPix(32));
        _dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _dateButton.titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0);
        [_dateButton addTarget:self action:@selector(selectDateAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dateButton;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTipDown12"]];
        _tipView.frame = CGRectMake(dateWidth-viewPix(18), viewPix(10), viewPix(12), viewPix(12));
    }
    return _tipView;
}

-(UITextField *)highTF{
    if(!_highTF){
        _highTF = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.dateButton.frame), kMinY(self.dateButton.frame), itemWidth, viewPix(32))];
        _highTF.tintColor = [UIColor colorWithString:@"#333333"];
        _highTF.textColor = [UIColor colorWithString:@"#333333"];
        _highTF.textAlignment = NSTextAlignmentCenter;
        _highTF.keyboardType = UIKeyboardTypeNumberPad;
        _highTF.placeholder = @"请输入";
        _highTF.font = LGFont(14);
        [_highTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _highTF;
}

-(UITextField *)lowTF{
    if(!_lowTF){
        _lowTF = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.highTF.frame), kMinY(self.dateButton.frame), itemWidth, viewPix(32))];
        _lowTF.tintColor = [UIColor colorWithString:@"#333333"];
        _lowTF.textColor = [UIColor colorWithString:@"#333333"];
        _lowTF.textAlignment = NSTextAlignmentCenter;
        _lowTF.keyboardType = UIKeyboardTypeNumberPad;
        _lowTF.placeholder = @"请输入";
        _lowTF.font = LGFont(14);
        [_lowTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _lowTF;
}

-(UITextField *)heartTF{
    if(!_heartTF){
        _heartTF = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.lowTF.frame), kMinY(self.dateButton.frame), heartWidth, viewPix(32))];
        _heartTF.tintColor = [UIColor colorWithString:@"#333333"];
        _heartTF.textColor = [UIColor colorWithString:@"#333333"];
        _heartTF.textAlignment = NSTextAlignmentCenter;
        _heartTF.keyboardType = UIKeyboardTypeNumberPad;
        _heartTF.placeholder = @"请输入";
        _heartTF.font = LGFont(14);
        [_heartTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _heartTF;
}

-(UIView *)segmentView{
    if(!_segmentView){
        _segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, kMaxY(self.dateTitle.frame), Screen_W-viewPix(56), 1)];
        _segmentView.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _segmentView;
}

-(UIView *)lineView1{
    if(!_lineView1){
        _lineView1 = [[UIView alloc] initWithFrame:CGRectMake(dateWidth, 0, 1, viewPix(68))];
        _lineView1.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _lineView1;
}

-(UIView *)lineView2{
    if(!_lineView2){
        _lineView2 = [[UIView alloc] initWithFrame:CGRectMake(kMaxX(self.highTitle.frame), 0, 1, viewPix(68))];
        _lineView2.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _lineView2;
}

-(UIView *)lineView3{
    if(!_lineView3){
        _lineView3 = [[UIView alloc] initWithFrame:CGRectMake(kMaxX(self.lowTitle.frame), 0, 1, viewPix(68))];
        _lineView3.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _lineView3;
}


@end
